<?php

namespace App\Services;

use App\Models\Person;
use Illuminate\Support\Facades\Log;
use App\Models\RsSendingMachineMsg;
use Illuminate\Support\Facades\Http;

class MessageService
{
    /**
     * 获取到用户的姓名
     * @param int $userPersonId
     * @return string
     */
    public function getUserName(int $userPersonId) : string
    {
        return Person::where('id',$userPersonId)
            ->value('name');
    }

    /**
     * 根据名称获取到webhook信息
     * @param string $name
     * @return string
     */
    public function getWebHookUrl(string $name) : ?string
    {
        return RsSendingMachineMsg::where('name',$name)
            ->where('status',1)
            ->value('webhook');
    }

    /**
     * 根据类型获取到
     * @param int $type
     * @param array $parameters
     * type = 1 报工申报 type = 2 报工审批 type = 3 服务单审批
     * @return string
     */
    public function getContent(int $type,array $parameters) : string
    {
        switch ($type) {
            case 1:
                $content =  "报工填报<font color=\"warning\">".date("Y年m月d日")."</font>，请相关同事注意及时审批。\n
                             >项目名称:<font color=\"comment\">".$parameters['project']."</font>
                             >任务名称:<font color=\"comment\">".$parameters['task']."</font>
                             >填报人:<font color=\"comment\">".$this->getUserName(auth('api')->user()->id)."</font>";
                break;
            case 2:
                $content =  "报工审批<font color=\"warning\">".date("Y年m月d日")."</font>，请相关同事留意审批内容。\n
                             >项目名称:<font color=\"comment\">".$parameters['project']."</font>
                             >审批状态:<font color=\"comment\">".$parameters['status']."</font>
                             >审批人:<font color=\"comment\">".$this->getUserName(auth('api')->user()->id)."</font>";
                break;
            default:
                $content =  "服务单节点扭转<font color=\"warning\">".date("Y年m月d日")."</font>，请相关同事及时审批。
                             >申报人:<font color=\"comment\">".$this->getUserName(auth('api')->user()->id)."</font>";
        }

        return $content;
    }

    /**
     * 消息发送
     * @param string $name
     * @param int $type
     * @param array $parameters
     */
    public function sendMessage(string $name,int $type,array $parameters)
    {
        try {
            $url = $this->getWebHookUrl($name);

            $content = $this->getContent($type,$parameters);

            if (!empty($url) && !empty($content)) {
                $response = Http::post($url, [
                    'msgtype' => 'markdown',
                    'markdown' => [
                        'content' => $content
                    ]
                ]);

                if (!$response->successful()) {
                    Log::channel('api')
                        ->info('群机器人消息发送异常：'.' url:'.$url.' content:'.$content);
                }
            }
        }catch (\Exception $exception){
            Log::channel('api')
                ->info('群机器人消息发送异常：'.$exception->getMessage());
        }
    }
}
